//使用机构活动模板
exports.editVoteActivity = function (req, res) {

    //获取restful参数
    var restfulParams = req.routeInfo.restfulParams;

    //最终结果
    var resultData = {};

    //任务流创建
    async.waterfall([
            //获取机构活动信息
            function (cb) {
                R.SEND_HTTP(
                    req,
                    {
                        url:CORE_APIConfig.coreAPI.searchOrganizationActivity+"/oa_id/"+restfulParams.oa_id+"/oa_type/1" ,
                        method: 'GET',
                    },
                    function (err,data) {
                        if(err || data.data == '' ){
                            return RES.response(res, false, data);
                        }

                        resultData.activity = data.data;
                        console.log(resultData.activity);
                        cb();
                    }
                )
            },
            //查询音乐信息
            function (cb) {
                if(resultData.activity[0].oa_music_id != 0){
                    R.SEND_HTTP(
                        req,
                        {
                            url:CORE_APIConfig.coreAPI.searchActivityMusic+"/am_id/"+resultData.activity[0].oa_music_id ,
                            method: 'POST',
                            data : {
                                fields:[
                                    "am_id",
                                    "am_url",
                                    "am_tag",
                                    "am_name"
                                ].join(",")
                            }

                        },
                        function (err,data) {
                            if(err || data.data == '' ){
                                return RES.response(res, false, data);
                            }
                            console.log(2);
                            resultData.musicData = data.data;
                            cb();
                        }
                    )
                }else{
                    resultData.musicData = [];
                    cb();
                }
            },
            //查询皮肤信息
            function (cb) {
                if(resultData.activity[0].oa_skin_id != 0 ){
                R.SEND_HTTP(
                    req,
                    {
                        url:CORE_APIConfig.coreAPI.searchActivitySkin+"/as_id/"+resultData.activity[0].oa_skin_id ,
                        method: 'POST',
                        data : {
                            fields:[
                                "as_bg_style",
                                "as_border_style",
                                "as_header_style",
                                "as_footer_style",
                                "as_tag"
                            ].join(",")
                        }

                    },
                    function (err,data) {
                        if(err || data.data == '' ){
                            return RES.response(res, false, data);
                        }
                        console.log(3);
                        resultData.skinData = data.data;
                        cb();
                    }
                )
                }else{
                    resultData.skinData = [];
                    cb();
                }
            },
            //机构活动拓展
            function (cb) {
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.searchVoteActivityConfig+"/vac_oaid/"+restfulParams.oa_id ,
                        method: 'GET',

                    },
                    function (err, data) {
                        //判断是否有错误信息
                        //console.log(data);
                        if (err || data.data == '') {
                            return RES.response(res, false, data);
                        }
                        resultData.config = data.data;
                        cb();
                    }
                );

            },

            //机构模板活动内容
            function (cb) {
                R.SEND_HTTP(
                    req,
                    {
                        url:CORE_APIConfig.coreAPI.searchVoteActivityContent+"/vac_oaid/"+restfulParams.oa_id+"/sort_order/vac_order-asc" ,
                        method: 'POST',
                        data : {
                            fields:[
                                "vac_type as type",
                                "vac_content as content"
                            ].join(",")
                        }
                    },
                    function (err,data) {
                        if(err){
                            return cb(err,data);
                        }
                        resultData.content = data.data;
                        cb();
                    }
                );
            },
            //机构模板活动奖励
            function (cb) {
                R.SEND_HTTP(
                    req,
                    {
                        url:CORE_APIConfig.coreAPI.searchVoteActivityReward+"/vr_oaid/"+restfulParams.oa_id+"/sort_order/vr_rank-asc" ,
                        method: 'POST',
                        data : {
                            fields:[
                                "vr_rank as rank",
                                "vr_coupon_id as coupon_id",
                                "vr_coupon_type as vr_coupon_type",
                                "vr_ranknum as ranknum",
                                "vr_name as name",
                                "vr_id as id"
                            ].join(",")
                        }
                    },
                    function (err,data) {
                        if(err){
                            return cb(err,data);
                        }
                        console.log(data.data);
                        resultData.reward = data.data;
                        cb();
                    }
                );
            },
        ], function (err, result) {

            //返回最终拼接结果
            RES.response(res, true, resultData);

        }
    );

}